<?php
/**
 * Created by PhpStorm.
 * User: aa
 * Date: 2019/9/3
 * Time: 19:31
 */

class Captcha
{
    public static function crate($width=80, $height=36) {
        $img = imagecreatetruecolor($width, $height);
        $black = imagecolorallocate($img, rand(0, 80), rand(0, 80), rand(0, 80));
        $green = imagecolorallocate($img, 0x00, 0xFF, 0x00);
        $white = imagecolorallocate($img, 0xFF, 0xFF, 0xFF);
        imagefill($img, 0, 0, $white);
        //生成随机的验证码
        $code = self::make();
        imagestring($img, 5, mt_rand($width/20,$width/8), mt_rand($height/20,$height/3), $code, $black);
        //加入噪点干扰
        for ($i = 0; $i < 200; $i++) {
        imagesetpixel($img, rand(0, 80), rand(0, 80), $black);
        imagesetpixel($img, rand(0, 80), rand(0, 80), $green);
        }
//        //加入线段干扰
//        for ($n = 0; $n <= 1; $n++) {
//        imageline($img, 0, rand(0, 10), 10, rand(0, 10), $black);
//        imageline($img, 0, rand(0, 10), 10, rand(0, 10), $white);
//        }
        //输出验证码
        header("content-type: image/png");
        $file = APP_PATH.'public/2.png';
        imagepng($img);
        //销毁图片
        imagedestroy($img);
        //生产随机验证码的函数
    }

    protected static function make()
    {
        $act = mt_rand(1,2);
        $code = '123456789';
        $a = substr(str_shuffle($code), 0, 1);
        $b = substr(str_shuffle($code), 0, 1);
        if($act == 1) {
            $_SESSION['count'] = $a+$b;
            return $a.'+'.$b.'=?';
        } else {
            $_SESSION['count'] = $a*$b;
            return $a.'x'.$b.'=?';
        }

    }


}